package com.rokyinfo.pdm.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.rokyinfo.pdm.entity.DDevice;
import com.rokyinfo.pdm.entity.DDistributionRecord;
import com.rokyinfo.pdm.entity.DModel;
import com.rokyinfo.pdm.mapper.DDeviceMapper;
import com.rokyinfo.pdm.service.IDDeviceService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.util.List;

/**
 * <p>
 * 设备表 服务实现类
 * </p>
 *
 * @author YuanZhiJian
 * @since 2017-01-22
 */
@Service
public class DDeviceServiceImpl extends ServiceImpl<DDeviceMapper, DDevice> implements IDDeviceService {

    @Value("${com.rokyinfo.pdm.downloadfiledirectory}")
    private String downloadFileDirectory;

    public String generateDistributionExcelFile(Long generatorId) throws Exception {

        DDistributionRecord mDDistributionRecord = new DDistributionRecord();
        mDDistributionRecord.setGeneratorId(generatorId);
        mDDistributionRecord = mDDistributionRecord.selectById();

        if (mDDistributionRecord == null) {
            throw new Exception("设备分配不存在");
        }

        DModel mDModel = new DModel();
        mDModel.setFirmId(mDDistributionRecord.getFirmId());
        mDModel = mDModel.selectById();

        if (mDModel == null) {
            throw new Exception("设备型号不存在");
        }

        DDevice mDDevice = new DDevice();
        mDDevice.setGeneratorId(generatorId);
        List<DDevice> list = baseMapper.selectList(new EntityWrapper<DDevice>(mDDevice));

        if (list == null || list.size() == 0) {
            throw new Exception("此分配记录下不存在设备");
        }

        XSSFWorkbook wb = new XSSFWorkbook();

        if (mDModel.haveBluetoothSpeciality() && mDModel.haveRadioSpeciality()) {
            // 有蓝牙又有移动通信
            XSSFSheet sheet = null;
            sheet = wb.createSheet(mDModel.getName() + "预分配表");
            XSSFRow row = sheet.createRow(0);

            XSSFCell cellOrderNum = row.createCell(0);
            cellOrderNum.setCellValue("OrderNum");

            XSSFCell cellSn = row.createCell(1);
            cellSn.setCellValue("SN");

            XSSFCell cellImei = row.createCell(2);
            cellImei.setCellValue("IMEI");

            XSSFCell cellBtAddr = row.createCell(3);
            cellBtAddr.setCellValue("BTaddr");

            XSSFCell cellBleAddr = row.createCell(4);
            cellBleAddr.setCellValue("BLEaddr");

            XSSFCell cellKey = row.createCell(5);
            cellKey.setCellValue("Key");

            for (int i = 0; i < list.size(); i++) {

                row = sheet.createRow(i + 1);
                DDevice preAllotUE = list.get(i);

                row.createCell(0).setCellValue(mDDistributionRecord.getGeneratorNum());
                row.createCell(1).setCellValue(preAllotUE.getSn());
                row.createCell(2).setCellValue(preAllotUE.getRadioInfo().getImei());
                row.createCell(3).setCellValue(preAllotUE.getBluetoothInfo().getMacAddress0().replaceAll(":", ""));
                row.createCell(4).setCellValue(preAllotUE.getBluetoothInfo().getMacAddress1().replaceAll(":", ""));
                row.createCell(5).setCellValue(preAllotUE.getBluetoothInfo().getEncryptKey());
            }
        } else if (mDModel.haveBluetoothSpeciality()) {

            // 只有蓝牙模块
            XSSFSheet sheet = null;
            sheet = wb.createSheet(mDModel.getName() + "预分配表");
            XSSFRow row = sheet.createRow(0);

            XSSFCell cellOrderNum = row.createCell(0);
            cellOrderNum.setCellValue("OrderNum");

            XSSFCell cellSn = row.createCell(1);
            cellSn.setCellValue("SN");

            XSSFCell cellBtAddr = row.createCell(2);
            cellBtAddr.setCellValue("BTaddr");

            XSSFCell cellBleAddr = row.createCell(3);
            cellBleAddr.setCellValue("BLEaddr");

            XSSFCell cellKey = row.createCell(4);
            cellKey.setCellValue("Key");

            for (int i = 0; i < list.size(); i++) {

                row = sheet.createRow(i + 1);
                DDevice preAllotUE = list.get(i);

                row.createCell(0).setCellValue(mDDistributionRecord.getGeneratorNum());
                row.createCell(1).setCellValue(preAllotUE.getSn());
                row.createCell(2).setCellValue(preAllotUE.getBluetoothInfo().getMacAddress0().replaceAll(":", ""));
                row.createCell(3).setCellValue(preAllotUE.getBluetoothInfo().getMacAddress1().replaceAll(":", ""));
                row.createCell(4).setCellValue(preAllotUE.getBluetoothInfo().getEncryptKey());
            }
        } else if (mDModel.haveRadioSpeciality()) {

            // 只有通信模块
            XSSFSheet sheet = null;
            sheet = wb.createSheet("RK600预分配表");
            XSSFRow row = sheet.createRow(0);

            XSSFCell cellOrderNum = row.createCell(0);
            cellOrderNum.setCellValue("OrderNum");

            XSSFCell cellSn = row.createCell(1);
            cellSn.setCellValue("SN");

            XSSFCell cellBtAddr = row.createCell(2);
            cellBtAddr.setCellValue("IMEI");

            for (int i = 0; i < list.size(); i++) {

                row = sheet.createRow(i + 1);
                DDevice preAllotUE = list.get(i);

                row.createCell(0).setCellValue(mDDistributionRecord.getGeneratorNum());
                row.createCell(1).setCellValue(preAllotUE.getSn());
                row.createCell(2).setCellValue(preAllotUE.getRadioInfo().getImei());
            }
        }

        String fileName = downloadFileDirectory
                + mDDistributionRecord.getGeneratorNum() + ".xlsx";

        FileOutputStream fout = new FileOutputStream(fileName);
        wb.write(fout);
        fout.close();

        return fileName;
    }

}
